package user_r

import (
	"frozen-go-mini/common/domain"
	"frozen-go-mini/common/mycontext"
	"frozen-go-mini/cv/user_cv"
	"frozen-go-mini/domain/user_m"
	"frozen-go-mini/resp"
	"github.com/gin-gonic/gin"
)

type UpdateUserReq struct {
	Id     uint64 `json:"id"`
	Nick   string `json:"nick"`
	Avatar string `json:"avatar"`
}

// @Tags 用户
// @Summary 更新用户信息
// @Param Authorization header string true "token"
// @Param UpdateUserReq body UpdateUserReq true "UpdateUserReq"
// @Success 200 {object} user_cv.User
// @Router /v1.0/user/update [POST]
func UpdateUserInfo(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	var param UpdateUserReq
	if err := c.ShouldBindJSON(&param); err != nil {
		return myCtx, err
	}
	if user, err := user_m.UpdateUserById(model, param.Id, param.Avatar, param.Nick); err != nil {
		return myCtx, err
	} else {
		resp.ResponseOk(c, user_cv.User{
			Id:     user.ID,
			Avatar: user.Avatar,
			Nick:   user.Nick,
		})
	}
	return myCtx, nil
}
